package com.xnf.ssmweb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageInterceptor;
import com.xnf.ssmweb.domain.Orders;
import com.xnf.ssmweb.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by chendx on 2021/3/29.
 */
@Controller
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @RequestMapping("/toOrders")
    public ModelAndView toOrders(Integer pageNum,Integer pageSize){
        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageSize = 5;
        ModelAndView mav = new ModelAndView();
        PageHelper.startPage(pageNum,pageSize);
        List<Orders> orders = ordersService.queryOrders();
        PageInfo<Orders> ordersPageInfo = new PageInfo<>(orders);
        mav.addObject("ordersPageInfo",ordersPageInfo);
        mav.setViewName("/orders-list");
        return mav;
    }

    @RequestMapping("/toOrderDetails")
    public ModelAndView toOrderDetails(Long id){
        ModelAndView mav = new ModelAndView();
        Orders orders = ordersService.findOrderById(id);
        mav.addObject("orders",orders);
        mav.setViewName("/orders-show");
        return mav;
    }
}
